<template>
  <div class="main-header">
    <el-dropdown>
      <div class="content">
        <div class="nickname-container">
          <span class="nickname">{{ user.nickname }}</span>
        </div>
        <div class="avatar-container">
          <template v-if="user.role.id === 1">
            <img
              class="avatar"
              src="@/assets/img/avatar_role_1.png"
            />
          </template>
          <template v-else>
            <img
              class="avatar"
              src="@/assets/img/avatar_role_2.png"
            />
          </template>
        </div>
        <el-icon
          color="#31a4ff"
          class="arrow-down-icon"
        >
          <CaretBottom />
        </el-icon>
      </div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="handleUserInfoClick">个人信息</el-dropdown-item>
          <el-dropdown-item @click="handleResetPasswordClick">修改密码</el-dropdown-item>
          <el-dropdown-item @click="handleExitClick">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup>
import { LOGIN_TOKEN_KEY } from '@/global/constants'
import { localCache } from '@/utils/cache'
import { useRouter } from 'vue-router'
import useLoginStore from '@/store/login/login'

const router = useRouter()

const loginStore = useLoginStore()
const user = loginStore.userInfo

function handleExitClick() {
	// 1.删除token
	localCache.removeCache(LOGIN_TOKEN_KEY)

	// 2.跳回登录页
	router.push('/login')
}

function handleUserInfoClick() {
  ElMessage({
    message: '开发中，敬请期待 ~',
    type: 'warning',
  })
}

function handleResetPasswordClick() {
  ElMessage({
    message: '开发中，敬请期待 ~',
    type: 'warning',
  })
}

</script>

<style lang="scss" scoped>
.main-header {
  padding: 8px 0;
  display: flex;
  justify-content: flex-end;
  .content {
    display: flex;
    align-items: center;
    cursor: pointer;
    .nickname {
      color: #31a4ff;
      font-size: 14px;
    }
    .avatar {
      display: block;
      margin-left: 10px;
      width: 40px;
      height: 40px; 
      border-radius: 50%;
      border: 2px solid #fff;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
      background-color: #dedede;
    }
    .arrow-down-icon {
      margin-left: 5px;
    }
  }
}
</style>